/*
 * @Author: szx
 * @Date: 2022-06-07 13:20:32
 * @LastEditTime: 2022-06-07 13:54:31
 * @Description:
 * @FilePath: \leetcode\面试题\16\08\08.js
 */
/**
 * @param {number} num
 * @return {string}
 */
var numberToWords = function (num) {
    const wordA = [
        '',
        'One',
        'Two',
        'Three',
        'Four',
        'Five',
        'Six',
        'Seven',
        'Eight',
        'Nine',
        'Ten',
        'Eleven',
        'Twelve',
        'Thirteen',
        'Fourteen',
        'Fifteen',
        'Sixteen',
        'Seventeen',
        'Eighteen',
        'Nineteen'
    ];
    const wordB = ['', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety'];
    const threeToWord = (num) => {
        const res = [];
        switch (num) {
            case num >= 100:
                res.push(Math.floor(num / 100), 'Hundred');
                num %= 100;
            case num >= 20:
                res.push(wordB[Math.floor(num / 10)]);
                num %= 10;
            case num > 0:
                res.push(wordA[num]);
        }
        return res.join(' ');
    };

    if (num == 0) return 'Zero';
    const res = [];
    switch (num) {
        case num >= 1000000000:
            res.push(threeToWord(Math.floor(num / 1000000000)), 'Billion');
            num %= 1000000000;
        case num >= 1000000:
            res.push(threeToWord(Math.floor(num / 1000000)), 'Million');
            num %= 1000000;
        case num >= 1000:
            res.push(threeToWord(Math.floor(num / 1000)), 'Thousand');
            num %= 1000;
        case num > 0:
            res.push(threeToWord(Math.floor(num)));
    }
    return res.join(' ');
};
